package org.twdata.pipeline.stage;

import java.io.*;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;

// SAX imports:
import org.apache.commons.logging.*;
import org.apache.commons.logging.*;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;

/**  Error handler for Trax error events  */
public class TraxErrorListener implements ErrorListener {

    /**  Logger */
    private static Log log = LogFactory.getLog(TraxErrorListener.class);


    /**  Constructor */
    public TraxErrorListener() { }


    /**
     *  Called for a transformer warning
     *
     *@param  exception  Exception that was thrown
     */
    public void warning(TransformerException exception) {
        log.error(exception.getMessage(), exception);

        // Don't throw an exception and stop the processor
        // just for a warning; but do log the problem
    }


    /**
     *  Called for a transformer error
     *
     *@param  exception                 The exception thrown
     *@exception  TransformerException  If anything goes wrong
     */
    public void error(TransformerException exception)
        throws TransformerException {
        log.error(exception.getMessage(), exception);

        // XSLT is not as draconian as XML. There are numerous errors
        // which the processor may but does not have to recover from;
        // e.g. multiple templates that match a node with the same
        // priority. I do not want to allow that so I throw this
        // exception here.
        throw exception;
    }


    /**
     *  Called for a fatal error
     *
     *@param  exception                 The exception thrown
     *@exception  TransformerException  If anything goes wrong
     */
    public void fatalError(TransformerException exception)
        throws TransformerException {
        log.fatal(exception.getMessage(), exception);
        // This is an error which the processor cannot recover from;
        // e.g. a malformed stylesheet or input document
        // so I must throw this exception here.
        throw exception;
    }
}


